package com.legal.service.practice.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.legal.domain.po.Question;
import com.legal.mapper.practice.QuestionMapper;
import com.legal.service.practice.IQuestionService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 题目表 服务实现类
 * </p>
 *
 * @author xiaopeng
 * @since 2025-06-28
 */
@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements IQuestionService {

    @Override
    public Map<String, Long> countByCategoryNos(List<String> categoryNos) {
        if (CollectionUtils.isEmpty(categoryNos)) {
            return Collections.emptyMap();
        }

        return this.listMaps(
                        new QueryWrapper<Question>()
                                .select("category_no, COUNT(*) as count")
                                .in("category_no", categoryNos)
                                .groupBy("category_no")
                ).stream()
                .collect(Collectors.toMap(
                        map -> (String) map.get("category_no"),
                        map -> (Long) map.get("count")
                ));
    }

    /**
     * 根据编号集合 查询题目列表
     * @param questionNos
     * @return 题目列表
     */
    @Override
    public List<Question> getQuestionsByNos(List<String> questionNos) {
        return lambdaQuery()
                .in(Question::getQuestionNo, questionNos)
                .list();
    }
}
